﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FTBS.Controllers
{
    public class ChairTypeController : Controller
    {
        ChairTypeBLO _chairTypeBO = new ChairTypeBLO();

        public ActionResult Index()
        {
            return View(_chairTypeBO.LoadChairTypes());
        }

        //--------------Create--------------------------
        public ActionResult Create()
        {
            this.Session["TypeName"] = null;
            return View();
        }

        [HttpPost]
        public ActionResult Create(ChairType chairType)
        {
            if (ModelState.IsValid)
            {
                if (!_chairTypeBO.CheckExistedChairType(chairType))
                {
                    this.Session["TypeName"] = null;
                    _chairTypeBO.CreateChairType(chairType);
                    return RedirectToAction("Index");
                }
                this.Session["TypeName"] = chairType.TypeName;
                return View(chairType);
            }
            return View(chairType);
        }

        //------------Edit------------------------------
        public ActionResult Edit(int cTypeId = 0)
        {
            ChairType chairType = _chairTypeBO.SelectChairType(cTypeId);
            if (chairType == null)
            {
                return HttpNotFound();
            }
            this.Session["curName"] = chairType.TypeName;
            this.Session["TypeName"] = null;
            return View(chairType);

        }

        [HttpPost]
        public ActionResult Edit(ChairType chairType)
        {
            if (ModelState.IsValid)
            {
                int compare = String.Compare(chairType.TypeName, (String)this.Session["curName"]);
                if (!_chairTypeBO.CheckExistedChairType(chairType) || (_chairTypeBO.CheckExistedChairType(chairType) && compare == 0))
                {
                    this.Session["TypeName"] = null;
                    _chairTypeBO.EditChairType(chairType);
                    return RedirectToAction("Index");
                }
                else if (_chairTypeBO.CheckExistedChairType(chairType) && compare != 0)
                {
                    this.Session["TypeName"] = chairType.TypeName;
                    return View(chairType);
                }
            }
            return View(chairType);
        }

        //------------------Delete---------------------
        [HttpDelete, ActionName("Delete")]
        public ActionResult Delete(int id = 0)
        {
            ChairType chairType = _chairTypeBO.SelectChairType(id);
            if (chairType == null)
            {
                return HttpNotFound();
            }
            _chairTypeBO.DeleteChairType(chairType);
            return RedirectToAction("Index");
        }

    }
}
